<?php
namespace Home\Controller;
use Think\Controller;
class CashCropListController extends Controller{
    public function index(){
        //设置查询条件
        $uc_card = I('get.uc_card')?I('get.uc_card'):0;
        //dump($uc_card);
        if($uc_card){
            $where['b.uc_card'] = $uc_card;
        }
        $page = 30;// 定义每页显示的数据量
        $p = I('get.p')?I('get.p'):1;
        $count      = M('Crops')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,$page);//例化分页类传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
    	$list = M('Crops')
    			->alias('a')
    			->join('LEFT JOIN __USER_CROPS__ b on a.uc_id = b.uc_id' )
    			->join('__CROPS_TYPE__ c on a.c_name = c.ct_id')
                ->where($where)
                ->order('a.c_time desc')
    			//->where('a.c_id=28')
                ->page($p.','.$page)
                //->fetchSql()
    			->select();
                //dump($list);
    	//var_dump($list);
    	$this ->assign('list',$list);
        $this->assign('page',$show);
        $this->display();
    }
    public function edit($c_id){
    	$rs = M('Crops')->where('c_id='.$c_id)->find();
    	$uc_id = $rs['uc_id'];
    	$user = M('User_crops')->where('uc_id='.$uc_id)->field('uc_name,uc_card')->find();
    	$rs['uc_name']=$user['uc_name'];
    	$rs['uc_card']=$user['uc_card'];
    	$this->assign('art',$rs);
    	$this->display();
    }
    public function save($c_id){
        $j = array();
        $data = array();  
        $data = I('post.');
        //var_dump($data);
        if(is_array($data['c_plant_type'])){
            $type = '';
            foreach ($data['c_plant_type'] as $k => $v) {
                //dump($v);
                switch ($v) {
                    case '庭院':
                        if($type){$type .= ',';}
                        $type .= $v.':'.$data['c_plant_patio'];
                        break;
                    case '露地':
                        if($type){$type .= ',';}
                        $type .= $v.':'.$data['c_plant_land'];
                        break;
                    case '设施-温室':
                        if($type){$type .= ',';}
                        $type .= $v.':'.$data['c_plant_house'];
                        break;
                    case '设施-冷棚':
                        if($type){$type .= ',';}
                        $type .= $v.':'.$data['c_plant_coldshed'];
                        break;
                }
            }
            $data['c_plant_type'] = $type;
        }
        //拼接存储方式
        if(is_array($data['c_storage_type'])){
            $data['c_storage_type']=$this ->stitching($data['c_storage_type']);
        }
        //置换
        // var_dump($data);
        // return false;
        //拼接销售方式
        if(is_array($data['c_sale_way'])){
            $way_type = '';
            foreach ($data['c_sale_way'] as $k => $v) {
                switch ($v) {
                    case '直销':
                        if($way_type){$way_type .= ',';}
                        $way_type .= $v.':'.$data['c_sale_way_zx_name'];
                        break;
                    case '中介':
                        if($way_type){$way_type .= ',';}
                        $way_type .= $v.':'.$data['c_sale_way_zj_name'];
                        break;
                }
            }
            $data['c_sale_way'] = $way_type;
        }
        //拼接销售市场
        if(is_array($data['c_sale_market'])){
            $mar_type = '';
            foreach ($data['c_sale_market'] as $k => $v) {
                switch ($v) {
                    case '本市':
                        if($mar_type){$mar_type .= ',';}
                        $mar_type .= $v.':'.$data['market_bshi'];
                        break;
                    case '本省':
                        if($mar_type){$mar_type .= ',';}
                        $mar_type .= $v.':'.$data['market_bs'];
                        break;
                    case '国内':
                        if($mar_type){$mar_type .= ',';}
                        $mar_type .= $v.':'.$data['market_gn'];
                        break;
                    case '境外':
                        if($mar_type){$mar_type .= ',';}
                        $mar_type .= $v.':'.$data['market_jw'];
                        break;
                }
            }
            $data['c_sale_market'] = $mar_type;
        }
        //拼接农产品认证标志
        if(is_array($data['c_authentication'])){
            $authen = '';
            foreach ($data['c_authentication'] as $k => $v) {
                switch ($v) {
                    case '无公害农产品':
                        if($authen){$authen .= ',';}
                        $authen .= ('无公害农产品:'.'1');
                        break;
                    case '绿色食品':
                        if($authen){$authen .= ',';}
                        $authen .= ('绿色食品:'.$data['c_authentication_green']);
                        break;
                    case '有机食品':
                        if($authen){$authen .= ',';}
                        $authen .= ('有机食品:'.$data['c_authentication_yj']);
                        break;
                }
            }
            $data['c_authentication'] = $authen;
        }
        //拼接应季时间，反季时间
        if($data['yj_time']){$data['c_sale_time_yj'] = $data['yj_time'];}
        if($data['fj_time']){$data['c_sale_time_fj'] = $data['fj_time'];}
        //拼接应季价格，反季价格
        if($data['yj_money']){$data['c_sale_money_yj'] = $data['yj_money'];}
        if($data['fj_money']){$data['c_sale_money_fj'] = $data['fj_money'];}

        
        $data['c_time'] = time();
        //return false;
        $rs = M('Crops')->data($data)->where('c_id='.$c_id)->save();
        if($rs){
            $j['status'] = 100;
            $j['info'] = '修改农户信息成功';
        }else{
            $j['status'] = 101;
            $j['info'] = '修改农户信息失败，请联系管理员';
        }
        $this->ajaxReturn($j);	
    }
    //拼接字符串方法
    private function stitching($arr){
        $type = '';
        if(!is_array($arr)){return false;}
        foreach ($arr as $v) {
            if(!$type){$type = $v;}else{$type=$type.",".$v;}                       
        }
        $arr=$type;
        return $arr;
    }
}

